<?php

namespace App\Library\Service;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\PetLogic;
use App\Library\Logic\ShopImageLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Pet\PetAddBatchRequest;
use App\Library\Requests\Pet\PetAddRequest;
use App\Library\Requests\Pet\PetEditRequest;
use App\Library\Requests\Pet\PetPageRequest;
use Illuminate\Http\JsonResponse;

class PetService extends BaseService
{
    /**
     * @param PetLogic $logic
     */
    public function __construct(PetLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * 分页
     * @param PetPageRequest $request
     * @return JsonResponse
     */
    public function paging(PetPageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * detail
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function detail(SerialNumberRequest $request): JsonResponse
    {
        $pet = $this->logic->findBySerialNumber($request);
        if (is_null($pet) || $pet->user_id !== USERID || $pet->pet_is_delete === 1) return fail(433);
        $pet->images = ShopImageLogic::query()->where([
            'img_source_type' => 8,
            'img_id' => !is_null($pet->pet_content_img_id_json) ? json_decode($pet->pet_content_img_id_json, true) : [0],
        ])->get();
        return success($pet);
    }

    /**
     * 删除
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function delete(SerialNumberRequest $request): JsonResponse
    {
        $pet = $this->logic->findBySerialNumber($request);
        if (is_null($pet) || $pet->user_id !== USERID) return fail(433);
        $pet->pet_is_delete = 1;
        $pet->save();
        OperateLogLogic::add();
        return success();
    }

    /**
     * add
     * @param PetAddRequest $request
     * @return JsonResponse
     */
    public function add(PetAddRequest $request): JsonResponse
    {
        $this->logic->add($request);
        OperateLogLogic::add();
        return success();
    }


    /**
     * edit
     * @param PetEditRequest $request
     * @return JsonResponse
     */
    public function edit(PetEditRequest $request): JsonResponse
    {
        $pet = $this->logic->findBySerialNumber($request);
        if (is_null($pet) || $pet->user_id !== USERID) return fail(433);
        $this->logic->edit($request, $pet);
        OperateLogLogic::add();
        return success();
    }

    /**
     * addBatch
     * @param PetAddBatchRequest $request
     * @return JsonResponse
     */
    public function addBatch(PetAddBatchRequest $request): JsonResponse
    {
        for ($i = 0; $i < $request->num; $i++) {
            $array[] = [
                'userId' => USERID,
                'petCode' => $this->logic->petCode(),
                'serialNumber' => serialNumber(),
                'createdAt' => time(),
                'updatedAt' => time(),
            ];
        }
        $this->logic->insert($array ?? []);
        OperateLogLogic::add();
        return success();
    }
}
